<?php
class p_programme_tvbcom_category extends Model{
	public static function getTableName(){
		return 'p_programme-tvbcom_category';
	}

    public function set($object)
    {
        $this->programme_id = isset($object->programme_id) ? $object->programme_id : '';
        $this->category_id = isset($object->category_id) ? $object->category_id : '';
        $this->rank = isset($object->rank) ? $object->rank : '';
    }

	public function delBySql($id)
	{
		$sql = "delete from `".self::getTableName()."` where programme_id='{$id}'";
		return Model::$dbo["w"]->getDbHandle()->exec($sql);
	}

	public function getCategoryByProgrammeID($id)
	{
        $sql = "SELECT * FROM `".self::getTableName()."` a, tvbcom_category b 
            WHERE a.programme_id='{$id}' AND a.category_id=b.id
            ORDER BY a.rank";
		return Model::$dbo["r"]->findBySql(__class__, $sql);
	}

	/**
	 * get tvbcom category list by cpl category id
	 *
	 * @param int $cpl_category_id : cpl category id
	 * @used service/get_list.php
	 *
	 */
	public function getTvbcomCategory($cpl_category_id, $sub_cpl_category_id=0)
	{
		if ($sub_cpl_category_id == 0)
		{
			$sql = "select * from cpl_category a left join tvbcom_category b on a.tvbcom_category_id=b.id left join `tvbcom_category-language` c on b.id=c.tvbcom_category_id where language_id=1 and a.id='{$cpl_category_id}'";
		}
		else
		{
			$sql = "select * from cpl_category a left join tvbcom_category b on a.tvbcom_category_id=b.id left join `tvbcom_category-language` c on b.id=c.tvbcom_category_id where language_id=1 and a.parent_id='{$cpl_category_id}' and a.id='{$sub_cpl_category_id}'";
		}
		return Model::$dbo["r"]->findBySql(__class__, $sql);
	}

    public function getProgrammeTvbcomCategory($programme_id, $category_id)
    {
        $sql = "SELECT * FROM `p_programme-tvbcom_category` WHERE `programme_id`={$programme_id} AND `category_id`={$category_id}";
        $ret = Model::$dbo['r']->findBySql(__CLASS__, $sql);
        return isset($ret[0]) ? $ret[0] : FALSE;
    }
}
?>
